//index.js
//获取应用实例
var app = getApp();
var loadMore = function (that) {

  var page = that.data.page;
  var page_size = '6';
  var url = '/Wxpro/categuds?typeid=' + that.data.typeid + '&page=' + page + '&page_size=' + page_size;
  console.log(url);
  app.func.req(url, {}, function (res) {
    console.log(res);
    if (res.data) {
      var prolist = that.data.oplist;
      for (var i = 0; i < res.data.length; i++) {
        prolist.push(res.data[i]);
      }
      that.setData({
        lock: 1,
        oplist: prolist
      });
      page = parseInt(page) + parseInt(page_size);
      wx.hideLoading()

    } else {
      wx.hideLoading()
      if (page == '0') {
        if (res.code == '202') {
          that.setData({
            nolist: 1
          })
          wx.showToast({
            title: '无记录',
            icon: 'success',
            duration: 2000
          })
        }
      } else {
        if (res.code == '202') {
          wx.showToast({
            title: '加载完毕',
            icon: 'success',
            duration: 2000
          })
        }
      }


    }
    that.setData({
      page: page
    });
  });

}

Page({
  data: {
    imageheightH: 0,
    imagewidthW: 0,
    showView: false,

    oplist: [],
    ky: "",
    page: 0,
    lock: 1,


    curNav: 1,
    curSubNav: 1,
    currentTab: 0,
    boolean: true,
    sxopennub: 0,
    paixunub: 0,
    paixuname: '综合排序',
    ft: '',

  },

  onLoad: function (options) {

    //获取高度宽度等
    var that = this;
    var presentname = options.ntit
    wx.setNavigationBarTitle({
      title: presentname//页面标题参数
    })

    that.setData({
      typeid: options.typeid
    })
    loadMore(that);



    wx.getSystemInfo({
      success: function (res) {

        var pageWidth = res.windowWidth;
        var oneonone = pageWidth * 0.27;
        var oneantwo = pageWidth * 0.5 - 12;
        that.setData({
          imageheightH: oneonone - 6,
          imageheightTH: oneantwo,
        });

      }
    });


  },

  //列表形式切换
  onChangeShowState: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        var aa = res.windowWidth;
      }
    });
    that.setData({
      showView: (!that.data.showView)
    });

    if (!that.data.showView == true) {
      wx.getSystemInfo({
        success: function (res) {
          var aa = res.windowWidth;
          that.setData({
            imageheightH: aa * 0.27 - 6,
          });
        }
      });
      console.log('1' + !that.data.showView)

    } else {

      wx.getSystemInfo({
        success: function (res) {
          var aa = res.windowWidth;
          that.setData({
            imageheightH: aa * 0.5 - 12,
          });
        }
      });
    }

  },
  bindDownLoad: function () {
    console.log('aaaaaaaaaaaa')
    var that = this;
    var lock = that.data.lock;
    if (lock == '1') {
      that.setData({
        lock: 2
      })
      loadMore(that);
      console.log("第" + that.data.page / 5 + "页");
    }

  },
  //筛选 展开关闭
  onChangeshaixuan: function (e) {
    var that = this;
    var sxopen = e.target.dataset.values;
    console.log(sxopen);
    console.log(e)
    that.setData({
      sxopennub: sxopen,
      paixunub: 2,
    });
  },
  onChangeshaixuandata: function (e) {
    var that = this;
    var ft = e.target.dataset.ft;

    that.setData({
      ft: ft
    });

  },
  onChangeshaixuandatadone: function (e) {
    var that = this;
    var sxopen = e.target.dataset.values;
    that.setData({
      sxopennub: sxopen,
      paixunub: 2,
      url: '/Wxpro/categuds/typeid/' + that.data.curSubNav + "/filter/" + that.data.ft
    });
    app.func.req(that.data.url, {}, function (res) {
      if (res.code == '0') {
        that.setData({
          oplist: res.data,
          page: 5
        })
      }
    });

  },
  //排序 展开关闭
  onChangepaixu: function (e) {
    var that = this;
    var pxopen = e.target.dataset.values;

    // console.log(e.target.dataset.values);
    that.setData({
      paixunub: pxopen
    });
  },
  //排序切换 关闭，在这个时间里写选择排序后的方法和正常关闭的不冲突
  onChangepaixudata: function (e) {
    var that = this;
    var pxopen = e.target.dataset.values;
    var pxname = e.target.dataset.profile;
    console.log(e);
    console.log(e.target.dataset.profile);

    that.setData({
      paixunub: pxopen,
      sxopennub: 2,
      paixuname: pxname,
    });
    if (e.target.dataset.profile != '综合排序') {

      if (e.target.dataset.profile == '价格由低到高') {
        var rank = "asc";
      } else if (e.target.dataset.profile == '价格由高到低') {
        var rank = "desc";

      }
      that.setData({
        url: '/Wxpro/categuds/typeid/' + that.data.typeid + "/rank/" + rank
      })
      app.func.req(that.data.url, {}, function (res) {
        if (res.code == '0') {
          that.setData({
            oplist: res.data
          })
        }
      });

    }else{
      app.func.req('/Wxpro/categuds/typeid/' + that.data.typeid, {}, function (res) {
        if (res.code == '0') {
          that.setData({
            oplist: res.data
          })
        }
      });
    }




  }

})
